#pragma once

#include <string>
#include <map>
#include <time.h>

#include "Network.h"

// ================================================================================

/**
 *  The name resolver abstraction
 *
 *  @param ipAddr  the IP address to be resolved
 */

class NameResolver {
public:
  virtual std::string resolve(IPAddress ipAddr) = 0;
};

// ================================================================================

/**
 *  Wraps OS resolver to provide caching
 */

class CachingResolver : public NameResolver {
public:
  std::string resolve(IPAddress ipAddr);

private:
  struct CacheEntry {
    CacheEntry(const std::string& name_) {
      name = name_;
      cacheTime = time(NULL);
    }

    time_t cacheTime;
    std::string name;
  };
  typedef std::map<IPAddress, CacheEntry> Cache;

  std::string resolveNoCache(IPAddress ipAddr);  

  Cache cache;
};
